package com.jf.yytgd.config;

import com.jf.yytgd.interceptor.UserInterceptor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description: SpringMVC配置类
 * User:22954
 * Date:2025-01-15
 * Time:14:58
 */
@Setter
@ConfigurationProperties(prefix = "interceptor")
@Configuration
public class WebConfig {
    private List<String> userPathPatterns;
    private List<String> userExcludePatterns;
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(@NonNull InterceptorRegistry registry) {
                InterceptorRegistration registration = registry.addInterceptor(new UserInterceptor());
                if (userPathPatterns != null && !userPathPatterns.isEmpty())
                    registration.addPathPatterns(userPathPatterns);
                if (userExcludePatterns != null && !userExcludePatterns.isEmpty())
                    registration.excludePathPatterns(userExcludePatterns);
            }
        };
    }
}
